home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7864 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.3 KB  |  179 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: overloading new operator
  5. Message-ID: <marnoldDn0A8r.Ipu@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4g8air$6a0@newsbf02.news.aol.com>
  8. Date: Mon, 19 Feb 1996 04:38:51 GMT
  9. Sender: marnold@netcom20.netcom.com
  10.  
  11. cperkins@aol.com (C Perkins) writes:
  12.  
  13. >Does anyone have a simple sample of overriding the new operator? How do  I
  14. >use new placement with constructors that take multiple arguments?
  15.  
  16. Placement new (or any form of overloaded new) has nothing do with 
  17. how many arguments constructors have.
  18.  
  19. >I have a number of classes which have constructors that take multiple
  20. >arguments.  Is it possible to dynamically create objects of these classes
  21. >using placement?  Can anyone show me an example?  
  22.  
  23. Are you clear on what "placement new" is?  I'll admit some C++ books
  24. call overloaded new operators "placement" new, but there is really 
  25. only one classic form of placement new.  I'll try to clear this up for
  26. you...
  27.  
  28. Placement new usually refers to the form of operator new that takes
  29. a pointer to some "place" in memory and then simply returns that pointer.
  30. It doesn't actually perform any memory allocation.  It is typically
  31. defined like so...
  32.  
  33.    void* operator new(size_t size, void* p)
  34.       {
  35.       return p;
  36.       }
  37.  
  38. If your compiler is farily up to date, you'll find the declaration
  39. for this form of operator new in the header file <new.h>.  Because
  40. this form of new lets you explicity place an object in memory, it is
  41. known as "placement new".
  42.  
  43. This form allows you to write code like this...
  44.  
  45.    char my_memory[100];
  46.  
  47.    new(my_memory) MyObject;
  48.  
  49. ...which explicitly *places* the newed instance of MyObject into
  50. the array of bytes called my_memory.  This kind of fine control 
  51. over object construction is usually required in only the *rarest* 
  52. of circumstances and must be used carefully.
  53.  
  54. So, the mechanics of overloading operator new are simple.  The size 
  55. of the object getting newed is passed as the size_t parameter.  
  56. Additional parameters are passed as the 2nd, 3rd, etc. parameters.  
  57. In the above case, the address of my_memory arrives inside the 
  58. operator new as the void* parameter.  There can be any number of
  59. these extra parameters and the can have any type.
  60.  
  61. So, the above example is what most people think of when you say 
  62. "placement new".  Other forms are usually referred to as simply 
  63. "overloaded" new, but some C++ books calls all forms of new with 
  64. "extra" parameters placement new, even the ARM, and books by B.
  65. Stroubstrup, the creator of C++.  I think this is confusing, and
  66. I prefer to use the description "placement new" for only the 
  67. "classic" form and the description "overloaded new" for all other
  68. fomrs.  I hope you find that clearer too.
  69.  
  70.  
  71. Anyhow, from your other comments, it sounds like you are asking 
  72. about *overloading* operator new to actually perform some kind of 
  73. custom memory allocation, not about the classic form of placement 
  74. new.
  75.  
  76. For this, you need to simply provide you own global or class-
  77. specific operator new.
  78.  
  79. Here is an example of a global operator new...
  80.  
  81.    void* operator new(size_t size)
  82.       {
  83.       return malloc(size);
  84.       }
  85.  
  86. All I've done here is make operator new simply use the standard
  87. C library routine malloc() to get memory.  This, in essence, is 
  88. what most standard operator new's do.  To find out what your 
  89. standard new does, examine your compiler's run-time library source
  90. code or ask your compiler vendor.
  91.  
  92. NOTE: The real standard operator new does a few other things, like
  93. calling the new_handler() when memory is low, etc.. 
  94.  
  95. Also, it is very important (in this example, anyway), to make sure
  96. global operator *delete* does the right thing in relation to new.  
  97. With the above new, we'd probably also want to overload operator
  98. delete, like so...
  99.  
  100.    void operator delete(void* p)
  101.       { 
  102.       free(p);   // opposite of malloc()
  103.       }
  104.  
  105. You usually overload operator new and delete as a pair.
  106.  
  107. If you had some special dynamic memory allocator you wanted to use,
  108. you could do this....
  109.  
  110.    void* operator new(size_t size)
  111.       {
  112.       return GetMemFromMySpecialAllocator(size);
  113.       }
  114.  
  115. ...and likewise define an operator delete to do the reverse.
  116.  
  117. Basically, in a custom operator new, all you have to do is allocate 
  118. size bytes and return the pointer to them.  It has nothing to do
  119. with how many parameters an object's constructor takes.  Operator
  120. new is not even aware of the type of object being newed.  It's only
  121. task is to allocate memory.
  122.  
  123. To use an overloaded version of global operator new, just use new
  124. like usual...
  125.  
  126.    Object* p = new Object;
  127.  
  128. If Object's can be constructed with other parameters, just do...
  129.  
  130.    Object* p = new Object(1, 2, 3);
  131.  
  132. ...it doesn't affect how new is called.
  133.  
  134. I also mentioned class-specific operator new.  You use this when
  135. you want to provide custom memory allocation *only* for a specific 
  136. class of objects, not all objects, as you do with overloading 
  137. global operator new.
  138.  
  139. To do this, you simply define an operator new as a member of the
  140. specific class, like so...
  141.  
  142.    void* Object::operator new(size_t size)
  143.       { 
  144.       // return pointer to memory to hold a Object instance
  145.       }
  146.  
  147. Now, if you do this...
  148.  
  149.    Object* p = new Object;  
  150.  
  151. ...Object::operator new() is called.  But, if you do this...
  152.  
  153.    Foo* p = new Foo;
  154.  
  155. ...the global operator new is called (unless, of course, Foo has
  156. it's own class-specific operator new).  Object's class-specific
  157. operator new does not affect class Foo.
  158.  
  159. >If I overload new with placement, is
  160. >my task simply to allocate the memory, and will the compiler then handle
  161. >calling the correct constructor? 
  162.  
  163. I think, after reading the above, you'll understand my answer when
  164. I just say "yes".
  165.  
  166. >I've gone through nearly every book C++ I've got, and all mention
  167. >overloading new, but none have any examples.
  168.  
  169. Which books do you have?  Maybe we can recommend some better ones.
  170.  
  171. Regards,
  172. -------------------------------------------------------------------------
  173. Matt Arnold                       |        | ||| | |||| |  | | || ||
  174. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  175. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  176. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  177. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  178. -------------------------------------------------------------------------
  179.